﻿
using System.Windows.Input;
using AnyCAD.Foundation;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System;
using CommunityToolkit.Mvvm.Input;
using Rapid.CAX.Model;
using AnyCAD.NX.ViewModel;
using AnyCAD.NX.Common;
using RapidUI;
using Fluent;

namespace Rapid.CAX.ViewModel
{
    public class RapidMainViewModel : MainViewModel
    {

        public ICommand New { get; set; }
        public ICommand Undo { get; set; }
        public ICommand Redo { get; set; }
        public ICommand ImportSTEP { get; set; }

        public ListBox mOutputMessage;

        public RapidMainViewModel(ProjectBrowserViewModel projectViewModel, StackPanel propertyView, ListBox outputBox)
            :base(projectViewModel, propertyView)
        {
            New = new RelayCommand(()=>NewDocument());
            Undo = new RelayCommand(()=>OnUndo());
            Redo = new RelayCommand(()=>OnRedo());
            ImportSTEP = new RelayCommand(OnImportSTEP);
            mOutputMessage = outputBox;

            OutputMessage("Me","Welcome to use RapidCAX!");

            if(!ProcessManager.Instance.Start("pclserver", @"PclServer.exe",
                string.Format("--hostId anycad.pcl --slaveId pcl.anycad"),
                OutputMessage))
            {
                OutputMessage("PclServer", "Failed to execute PclServer.");
            }
        }

        void AddOutputBox(string msg)
        {
            mOutputMessage.Items.Add(msg);
        }
        public void OutputMessage(string name, string? msg)
        {
            if (msg == null)
                return;

            System.Windows.Application.Current.Dispatcher.Invoke(AddOutputBox, msg);
        }

        public override void ProjectSelectionChanged()
        {
            base.ProjectSelectionChanged();
            // 目录树里面选择了对象，在这里处理
        }

        public override void ViewSelectionChanged(PickedResult picked)
        {
            base.ViewSelectionChanged(picked);
            // 三维视图里面选择了对象，在这里处理
        }

        void OnImportSTEP()
        {
            var dlg = new Microsoft.Win32.OpenFileDialog
            {
                DefaultExt = ".stp",
                Filter = "STEP Files (*.stp;step)|*.stp;*.step|IGES Files (*.igs;*.iges)|*.igs;*.iges|STL Files (*.stl)|*.stl|BREP Files (*.brep)|*.brep"
            };
            var result = dlg.ShowDialog();
            if (result != true)
                return;

            var filename = dlg.FileName;
            var shape = ShapeIO.Open(filename);
            if (shape == null)
                return;

            SolidSchema.Create(dlg.SafeFileName, shape, Document);
        }

        public override Ribbon GetRibbonBar()
        {
            return ((MainWindow)App.Current.MainWindow).mRibbon;
        }

        public override void SetTitle(string title)
        {
            ((MainWindow)App.Current.MainWindow).Title = title;
        }
    }
}
